﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using WDTAsg2.DbBizObject;

namespace WDTAsg2.Admin
{
    public partial class UploadManager : System.Web.UI.Page
    {

        // Instance of EF
        private HardwaresRUsDBEntities HardwaresRUsDBContext;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBindList();
        }

        private void DataBindList()
        {
            //Initialize the ObjectContext
            HardwaresRUsDBContext = new HardwaresRUsDBEntities();

            var downloads = from d in HardwaresRUsDBContext.Downloads
                            select d;
            try
            {

                //((ObjectQuery)downloads).Execute(MergeOption.AppendOnly);
                this.lvDownloads.DataSource = downloads;
                lvDownloads.DataBind();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        protected void lvDownloads_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            ListViewItem item = e.Item;
            int idParam = Convert.ToInt32(((Label)item.FindControl("DownloadIdLabel")).Text);

            // Depending on command approve or dissaprove
            switch (e.CommandName)
            {
                case "Approve":
                    DownloadInfo.ApproveDownloadById(idParam, true);
                    break;

                case "Disapprove":
                    DownloadInfo.ApproveDownloadById(idParam, false);
                    break;

            }

            DataBindList();
        }

    }
}